package com.ycu.jk16.mall.dao;

import static com.ycu.jk16.mall.util.common.AndroidMalConstant.SHOPPINGCARTTABLE;

import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.mapping.FetchType;

import com.ycu.jk16.mall.dao.provider.ShoppingCartDynaSqlProvider;
import com.ycu.jk16.mall.domain.ShoppingCart;

public interface ShoppingCartDao {
	
	
	// 以userId查询改用户购物车里面的商品
	@Select("select * from " + SHOPPINGCARTTABLE + " where user_id = #{userId} ")
	@Results({ // 将JavaBean里的变量名与数据库字段名不相符的进行标注
			@Result(column = "id", property = "id", id = true),
			@Result(column = "user_id", property = "user", one = @One(select = "com.ycu.jk16.mall.dao.UserDao.selectById", fetchType = FetchType.EAGER)),
			@Result(column = "product_id", property = "product", one = @One(select = "com.ycu.jk16.mall.dao.ProductDao.selectById", fetchType = FetchType.EAGER)),
			@Result(column = "create_time", property = "createTime", javaType = java.util.Date.class)
			})
	List<ShoppingCart> selectShoppingCartByUserId(@Param("userId") Integer userId);
	
	
	//使用用户Id 和产品Id  查找购物中的记录
	@Select("select * from " + SHOPPINGCARTTABLE + " where user_id = #{userId} and product_id = #{productId} ")
	@Results({ // 将JavaBean里的变量名与数据库字段名不相符的进行标注
		@Result(column = "id", property = "id", id = true),
		@Result(column = "user_id", property = "user", one = @One(select = "com.ycu.jk16.mall.dao.UserDao.selectById", fetchType = FetchType.EAGER)),
		@Result(column = "product_id", property = "product", one = @One(select = "com.ycu.jk16.mall.dao.ProductDao.selectById", fetchType = FetchType.EAGER)),
		@Result(column = "create_time", property = "createTime", javaType = java.util.Date.class)
		})
	ShoppingCart selectShoppingCartByUserIdWithProductId(@Param("userId") Integer userId,
			@Param("productId") Integer productId);
	
	
	// 增加购物车中一条记录
	@Insert({ "insert into " + SHOPPINGCARTTABLE
			+ "(user_id, product_id,quantity,create_time) "
			+ "values(#{userId}, #{productId},#{quantity},#{createTime})" })
	void addShoppingCart(@Param("userId") Integer userId,
			@Param("productId") Integer productId,
			@Param("quantity") Integer quantity,
			@Param("createTime") Date createTime);
	
	// 动态修改购物车中的商品数量信息
	@SelectProvider(type = ShoppingCartDynaSqlProvider.class, method = "updateShoppingCartOfQuantity")
	void updateShoppingCartOfQuantity(ShoppingCart shoppingCart);

	@Delete("delete from " + SHOPPINGCARTTABLE + " where id = #{id} ")
	void deleteShoppingCart(@Param("id") Integer id);
	
	
	
	
}
